package task1;

import java.io.Serializable;

/**
 * 学生类
 */
public class Student implements Serializable {
    // 定义序列化版本号
    private static final long serialVersionUID = -5814716593800822421L;
    // 学号
    private String id;
    // 姓名
    private String name;
    // 年龄
    private int age;

    public Student() {
    }

    public Student(String id) {
        setId(id);
    }

    public Student(String id, String name, int age) {
        setId(id);
        setName(name);
        setAge(age);
    }

    public String getId() {
        return id;
    }

    public void setId(String id){
        // 学生id位数必须为6位
        if (id.length() == 6)
            this.id = id;
        else {
            try {
                throw new StudentIdException("学生ID格式不规范！");
            }catch (StudentIdException e){
                e.printStackTrace();
            }
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        // 学生年龄必须在18岁到100岁之间
        if (age >= 18 && age <= 100)
            this.age = age;
        else {
            try {
                throw new StudentAgeException("学生年龄不正确！");
            } catch (StudentAgeException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return "学号：" + id + "，姓名：" + name + "，年龄：" + age;
    }
}
